package com.diy.sigmund.diyuser.controller;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.entity.po.user.UserAuth;
import com.diy.sigmund.diycommon.entity.enumerate.VerifyParamPolicyEnum;
import com.diy.sigmund.diycommon.spring.anno.IgnoreTokenVerification;
import com.diy.sigmund.diycommon.spring.anno.VerifyParam;
import com.diy.sigmund.diycommon.spring.anno.VerifyParams;
import com.diy.sigmund.diyuser.entity.dto.UserAuthDTO;
import com.diy.sigmund.diyuser.service.UserAuthService;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户授权控制类
 *
 * @author ylm-sigmund
 * @since 2021/8/19 17:09
 */
@RestController
@RequestMapping("/user/userAuth")
public class UserAuthController {

    @Autowired
    private UserAuthService userAuthService;

    @PostMapping("/selectByUserId")
    public ResultMsg selectByUserId(@RequestBody String userId) {
        final List<UserAuth> userAuths = userAuthService.selectByUserId(userId);
        return ResultMsg.success(userAuths);
    }

    /**
     * @api {POST} /user/userAuth/register register
     * @apiVersion 1.0.0
     * @apiGroup UserAuthController
     * @apiName register
     * @apiDescription 用户注册，入库用户授权表和用户基础信息表
     * @apiParam (请求体) {String} identityType 1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博
     * @apiParam (请求体) {String} isExternalLogin 0 站内登录方式，1 站外登录方式
     * @apiParam (请求体) {String} identifier 手机号 邮箱 用户名或第三方应用的唯一标识
     * @apiParam (请求体) {String} credential 密码凭证(站内的保存密码，站外的不保存或保存token)
     * @apiParamExample 请求体示例
     * {"identifier":"laoLzy5q","isExternalLogin":"nPu0b","credential":"UqDH","identityType":"xPvKsH"}
     * @apiSuccess (响应结果) {String} code 响应码，200表示成功，其它表示失败
     * @apiSuccess (响应结果) {String} msg 提示信息
     * @apiSuccess (响应结果) {Object} data 响应数据
     * @apiSuccessExample 响应结果示例
     * {"msg":"成功","code":"200","data":{}}
     * @apiErrorExample 失败结果示例
     * {"msg":"访问请求处理失败","code":"99","data":{}}
     */
    @IgnoreTokenVerification
    @PostMapping("/register")
    @VerifyParams(value = {
            @VerifyParam(fieldName = "identityType", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "身份类型不能为空"),
            @VerifyParam(fieldName = "isExternalLogin", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "是否站内登录不能为空"),
            @VerifyParam(fieldName = "identifier", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "注册唯一标识不能为空"),
            @VerifyParam(fieldName = "credential", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "密码不能为空")})
    public ResultMsg register(@RequestBody UserAuthDTO dto) {
        return userAuthService.register(dto);
    }

    /**
     * @api {POST} /user/userAuth/login login
     * @apiVersion 1.0.0
     * @apiGroup UserAuthController
     * @apiName login
     * @apiDescription 用户登录
     * @apiParam (请求体) {String} identityType 1手机号 2邮箱 3用户名 4qq 5微信 6腾讯微博 7新浪微博
     * @apiParam (请求体) {String} isExternalLogin 0 站内登录方式，1 站外登录方式
     * @apiParam (请求体) {String} identifier 手机号 邮箱 用户名或第三方应用的唯一标识
     * @apiParam (请求体) {String} credential 密码凭证(站内的保存密码，站外的不保存或保存token)
     * @apiParamExample 请求体示例
     * {"identifier":"Qz5spE","isExternalLogin":"TZ0xA","credential":"OhQthmoGz","identityType":"5"}
     * @apiSuccess (响应结果) {String} code 响应码，200表示成功，其它表示失败
     * @apiSuccess (响应结果) {String} msg 提示信息
     * @apiSuccess (响应结果) {Object} data 响应数据
     * @apiSuccessExample 响应结果示例
     * {"msg":"成功","code":"200","data":{}}
     * @apiErrorExample 失败结果示例
     * {"msg":"访问请求处理失败","code":"99","data":{}}
     */
    @IgnoreTokenVerification
    @PostMapping("/login")
    @VerifyParams(value = {
            @VerifyParam(fieldName = "identityType", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "身份类型不能为空"),
            @VerifyParam(fieldName = "isExternalLogin", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "是否站内登录不能为空"),
            @VerifyParam(fieldName = "identifier", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "注册唯一标识不能为空"),
            @VerifyParam(fieldName = "credential", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "密码不能为空")})
    public ResultMsg login(@RequestBody UserAuthDTO dto) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userAuthService.login(dto);
    }

    /**
     * @api {POST} /user/userAuth/signOut signOut
     * @apiVersion 1.0.0
     * @apiGroup UserAuthController
     * @apiName signOut
     * @apiDescription 登出，清除缓存
     * @apiSuccess (响应结果) {String} code 响应码，200表示成功，其它表示失败
     * @apiSuccess (响应结果) {String} msg 提示信息
     * @apiSuccess (响应结果) {Object} data 响应数据
     * @apiSuccessExample 响应结果示例
     * {"msg":"成功","code":"200","data":{}}
     * @apiErrorExample 失败结果示例
     * {"msg":"访问请求处理失败","code":"99","data":{}}
     * @apiHeader {String} token Users unique accessToken.
     */
    @PostMapping("/signOut")
    public ResultMsg signOut() {
        return userAuthService.signOut();
    }

    /**
     * @api {POST} /user/userAuth/logout logout
     * @apiVersion 1.0.0
     * @apiGroup UserAuthController
     * @apiName logout
     * @apiDescription 用户注销，删除用户数据
     * @apiSuccess (响应结果) {String} code 响应码，200表示成功，其它表示失败
     * @apiSuccess (响应结果) {String} msg 提示信息
     * @apiSuccess (响应结果) {Object} data 响应数据
     * @apiSuccessExample 响应结果示例
     * {"msg":"成功","code":"200","data":{}}
     * @apiErrorExample 失败结果示例
     * {"msg":"访问请求处理失败","code":"99","data":{}}
     * @apiHeader {String} token Users unique accessToken.
     */
    @PostMapping("/logout")
    public ResultMsg logout() {
        return userAuthService.logout();
    }

    /**
     * @api {POST} /user/userAuth/resetCredential resetCredential
     * @apiVersion 1.0.0
     * @apiGroup UserAuthController
     * @apiName resetCredential
     * @apiDescription 站内密码重置
     * @apiParam (请求体) {String} identifier 手机号 邮箱 用户名或第三方应用的唯一标识
     * @apiParamExample 请求体示例
     * {"identifier":"5s"}
     * @apiSuccess (响应结果) {String} code 响应码，200表示成功，其它表示失败
     * @apiSuccess (响应结果) {String} msg 提示信息
     * @apiSuccess (响应结果) {Object} data 响应数据
     * @apiSuccessExample 响应结果示例
     * {"msg":"成功","code":"200","data":{}}
     * @apiErrorExample 失败结果示例
     * {"msg":"访问请求处理失败","code":"99","data":{}}
     * @apiHeader {String} token Users unique accessToken.
     */
    @PostMapping("/resetCredential")
    @VerifyParams(value = @VerifyParam(fieldName = "credential", verifyPolicy = VerifyParamPolicyEnum.NOT_BLANK, msg = "密码不能为空"))
    public ResultMsg resetCredential(@RequestBody UserAuthDTO dto) {
        return userAuthService.resetCredential(dto);
    }

    /** token的刷新放在网关服务 */
    public ResultMsg refreshToken(@RequestBody UserAuthDTO dto) {
        return null;
    }
}
